/*
 * AdminBean.java
 */
package ITU_ShuttleTracker.Admin;

/**
 *
 * @author Onuralp
 */
import ITU_ShuttleTracker.Database;
import ITU_ShuttleTracker.User.ShuttleBus.ShuttleBus;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
    
@ManagedBean(name="AdminBean")
@SessionScoped
public class AdminBean {
    public String username;
    public String password;
    public ShuttleBus shuttleBus;
    public List<ShuttleBus> shuttleBusList;
    public ShuttleBus newShuttleBus;
    
    public AdminBean() {
	this.username = "";
	this.password = "";
        this.session=false;
        this.shuttleBus = new ShuttleBus();
        this.shuttleBusList = new ArrayList<ShuttleBus>();
        this.newShuttleBus = new ShuttleBus();
    }

    public boolean isSession() {
        return session;
    }

    public void setSession(boolean session) {
        this.session = session;
    }
    public boolean session;
           
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public ShuttleBus getShuttleBus() {
        return shuttleBus;
    }

    public void setShuttleBus(ShuttleBus shuttleBus) {
        this.shuttleBus = shuttleBus;
    }

    public List<ShuttleBus> getShuttleBusList() {
        return shuttleBusList;
    }

    public void setShuttleBusList(List<ShuttleBus> shuttleBusList) {
        this.shuttleBusList = shuttleBusList;
    }

    public ShuttleBus getNewShuttleBus() {
        return newShuttleBus;
    }

    public void setNewShuttleBus(ShuttleBus newShuttleBus) {
        this.newShuttleBus = newShuttleBus;
    }

    public String login(ActionEvent actionEvent){
        if(username.equals("admin") && password.equals("1234")){
	try {
            this.session = true;
            manageShuttleBus();
            FacesContext.getCurrentInstance().getExternalContext().redirect("adminMainPG.xhtml");
	} catch (IOException e) {
            System.out.println(e.toString());
        }
        return "";
	}
	else {
            //FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Kullanıcı adı veya parola yanlış"));
            //FacesContext.getCurrentInstance().addMessage("adminloginpanel:password", new FacesMessage("aasdasdsd"));
            FacesContext.getCurrentInstance().addMessage("adminloginpanel:password", new FacesMessage("Kullanıcı adı veya parola yanlış"));
            return "";
	}
    }
    
    public String logout(ActionEvent actionEvent) {
        try {
            this.session = false;
            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
        } catch (IOException e) {
            System.out.println(e.toString());
        }
        return "";
    }
    
    public void manageShuttleBus() {
        Database database = new Database();
    	try {
            database.connect();
            this.shuttleBusList = database.selectShuttleBus();
            database.disconnect();
	} catch (Exception e) {
            System.out.println(e.toString());
	}
    }
    
    public String createShuttleBus() {
    	Database database = new Database();
	try {
            database.connect();
            database.addShuttleBus(this.newShuttleBus);
            database.disconnect();
            manageShuttleBus();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Mekik servis bilgileri eklendi."));
            FacesContext.getCurrentInstance().getExternalContext().redirect("adminMainPG.xhtml");
	} catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Ekleme işlemi başarısız."));
            System.out.println(e.toString());
	}
	return "";
    }
    
    public String removeShuttleBus() {
        Database database = new Database();
    	try {
            database.connect();
            database.deleteShuttleBus(this.shuttleBus);
            database.disconnect();
            manageShuttleBus();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Mekik servis bilgileri kaldırıldı."));
            FacesContext.getCurrentInstance().getExternalContext().redirect("adminMainPG.xhtml");
	} catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Kaldırma işlemi başarısız."));
            System.out.println(e.toString());
	}
    	return "";
    }
    
    public String editShuttleBus() {
    	Database database = new Database();
    	try {
            database.connect();
            database.updateShuttleBus(shuttleBus);
            database.disconnect();
            manageShuttleBus();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Mekik servis bilgileri düzeltildi."));
            FacesContext.getCurrentInstance().getExternalContext().redirect("adminMainPG.xhtml");
	} catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Düzeltme işlemi başarısız."));
            System.out.println(e.toString());
	}
    	return "";
    }

    public String addPage(ActionEvent actionEvent) {
        try {
             FacesContext.getCurrentInstance().getExternalContext().redirect("adminAddShuttle.xhtml");
        } catch (IOException e) {
            System.out.println(e.toString());
        }
        return "";
    }
    
    public String updatePage(ActionEvent actionEvent) {
        try {
             FacesContext.getCurrentInstance().getExternalContext().redirect("adminUpdateShuttle.xhtml");
        } catch (IOException e) {
            System.out.println(e.toString());
        }
        return "";
    }
    

}